package voxbox.wrapper;

import processing.core.PApplet;
import processing.core.PMatrix;
import processing.core.PVector;
import voxietest1.VoxiObject;

import com.sun.jna.Native;

public abstract class VoxiScene extends VoxiObject {

	public VoxiFrame frame;
	public VoxiWindT window;
	public VoxiInputsT inputs;
	public PolT pt_addr;

	protected double tim = 0.0, otim;

	public double deltaTime;
	protected int i = 0;
	public int mouseX = 0, mouseY = 0, mouseZ = 0;
	public int mouseDX = 0, mouseDY = 0, mouseDZ = 0;
	private boolean _isAlive = false;
	
	public boolean isClick = false;

	public VoxiScene() {
	}

	public void init() {
		window = new VoxiWindT();
		frame = new VoxiFrame();
		inputs = new VoxiInputsT();
		pt_addr = new PolT();

		voxi = (VoxiNativeLibWrapper) Native.loadLibrary("voxiebox",
				VoxiNativeLibWrapper.class);
		
		voxi.voxi_loadini_int(window); // get settings from KENTEST.INI. May
		// override settings in vw structure
		// here if desired.
		window.useemu = 0;
		window.voxie_aud = 0;
		window.projrate = 60;
		window.freq = 20.0037;
		window.phase = 0.16;
		window.drawboth = 0;
		window.hidewind = 1; 
				
		window.proj0.x = 102.02f;
		window.proj0.y=25.07f;
		window.proj1.x=71.44f;
		window.proj1.y=37.09f;
		window.proj2.x=407.20f;
		window.proj2.y=32.61f;
		window.proj3.x=438.00f;
		window.proj3.y=38.29f;
		window.proj4.x=396.99f;
		window.proj4.y=555.62f;
		window.proj5.x=421.94f;
		window.proj5.y=667.95f;
		window.proj6.x=113.99f;
		window.proj6.y=555.17f;
		window.proj7.x=92.33f;
		window.proj7.y=677.45f;
		window.aspx = 2.0f;
		window.aspy = 1.0f;
		window.aspz = 2.5f/4.0f;
		
		voxi.voxi_init(window); // Start video and audio.
		
		_isAlive = true;
	}
	
	public void setup(){
		init();
		mySetup();
	}

	public boolean isAlive() {
		return _isAlive;
	}
	
	public void update() {
		voxi.voxi_breath(inputs);
		
		otim = tim;
		tim = voxi.voxi_klock();
		deltaTime = tim - otim;
		
		mouseDX = inputs.dmousx;
		mouseDY = inputs.dmousy;
		mouseDZ = inputs.dmousz;
		mouseX += inputs.dmousx;
		mouseY += inputs.dmousy;
		mouseZ += inputs.dmousz;

		if ((inputs.bstat&1) > (inputs.obstat&1)){
			isClick = true;
		} else {
			isClick = false;
		}
		
		if (voxi.voxi_keystat(0x1) != 0) {
			voxi.voxi_quitloop();
			_isAlive = false;
		}

		if (voxi.voxi_keystat(0x39) == 1) {
			voxi.voxi_playsound(String.format("c:/windows/media/tada.wav"),
					0, 100, 100, 1.0f);
		}

		i = (voxi.voxi_keystat(0x1b) & 1) - (voxi.voxi_keystat(0x1a) & 1);
		if (i != 0) {
			if (voxi.voxi_keystat(0x2a) + voxi.voxi_keystat(0x36) != 0)
				window.emuvang = Math.min(Math.max(window.emuvang + (float) i * deltaTime
						* 2.0, -Math.PI * .5), 0.1268); // Shift+[,]
			else if (voxi.voxi_keystat(0x1d) + voxi.voxi_keystat(0x9d) != 0)
				window.emudist = Math.max(window.emudist - (float) i * deltaTime * 2048.0,
						2000.0); // Ctrl+[,]
			else
				window.emuhang += (float) i * deltaTime * 2.0; // [,]

			voxi.voxi_init(window);
		}

		voxi.voxi_frame_start(frame);
		voxi.voxi_setview(frame, -window.aspx, -window.aspy, -window.aspz, window.aspx,
				window.aspy, window.aspz);

		myUpdate();
		myDraw();
		
		voxi.voxi_frame_end();
	}

	public void exit() {
		voxi.voxi_uninit_int();
	}
	
	public void drawLine(PVector p0, PVector p1) {
		PMatrix m = pApplet.getMatrix();
		float[] in0 = new float[4];
		float[] in1 = new float[4];
		float[] out0 = new float[4];
		float[] out1 = new float[4];
		
		in0[0] = p0.x;
		in0[1] = p0.y;
		in0[2] = p0.z;
		in0[3] = 1;
		
		in1[0] = p1.x;
		in1[1] = p1.y;
		in1[2] = p1.z;
		in1[3] = 1;
		
		m.mult(in0, out0);
		m.mult(in1, out1);
		voxi.voxi_drawlin(frame, out0[0], out0[1], out0[2], out1[0], out1[1], out1[2], 7);
	}
	
	public void drawBox(float cx, float cy, float cz, float angle, float width, float depth) {
		pApplet.resetMatrix();
		pApplet.pushMatrix();
		pApplet.rotateZ(angle);
		pApplet.translate(-cx, -cy, -cz);
		
		drawLine(new PVector(width/2, depth/2, 0), new PVector(-width/2, depth/2, 0));
		drawLine(new PVector(width/2, -depth/2, 0), new PVector(-width/2, -depth/2, 0));
		drawLine(new PVector(width/2, -depth/2, 0), new PVector(width/2, depth/2, 0));
		drawLine(new PVector(-width/2, -depth/2, 0), new PVector(-width/2, depth/2, 0));
		//drawLine(new PVector(width/2, depth/2, 0), new PVector(-width/2, -depth/2, 0));
		//drawLine(new PVector(width/2, -depth/2, 0), new PVector(-width/2, depth/2, 0));
		drawLine(new PVector(0, -depth/2, 0), new PVector(0, depth/2, 0));
		drawLine(new PVector(-width/2, 0, 0), new PVector(width/2, 0, 0));
		
		pApplet.popMatrix();
	}

}